/**
 * Created by Administrator on 15-6-26.
 */
var Query = RichBase.extend({
	statics : {
		currentPage : 0,
		last : "",
		oData : {},
		dData : {},
		queryState : {
			loading : '<li class="loading" style="text-align:center; line-height:1; height:400px;"><p style="color:#aeaeae; font-size:12px; padding-top:300px;">加载数据...</p><img src="images/other/loading_1.gif" alt="" /></li>',
			empty : '<li class="loading" style="text-align:center; line-height:1; height:400px;"><p style="color:#aeaeae; font-size:12px; padding-top:300px;">没有产品...</p></li>',
			fail : '<li class="loading" style="text-align:center; line-height:1; height:400px;"><p style="color:#aeaeae; font-size:12px; padding-top:300px;">请求失败...</p></li>',
			unlogin : '<li class="loading" style="text-align:center; line-height:1; height:400px;"><p style="color:#aeaeae; font-size:12px; padding-top:300px;">登录过期，请重新登录...</p></li>',
			timeout : '<li class="loading" style="text-align:center; line-height:1; height:400px;"><p style="color:#aeaeae; font-size:12px; padding-top:300px;">网络请求超时...</p></li>',
			serverError : '<li class="loading" style="text-align:center; line-height:1; height:400px;"><p style="color:#aeaeae; font-size:12px; padding-top:300px;">请求出错...</p></li>'
		},
		getProv : function(id){
			var prov = "";
			var p = PROVINCES;
			if(!id) return prov;
			for(var i in p){
				var areaid = p[i]["area_id"];
				if(areaid==id){
					prov = p[i]["area_name"];
					break;
				}
			}
			return prov;
		},
		isObjEmpty : function(obj){
			for(var i in obj){
				return false;
			}
			return true;
		}
	},
	EVENTS : {
		"click" : {
			"#pagenavW .pageBtn" : "onPrevNextClick"
		}
	},
	init : function(opt){
		this.prevBtn = $("#prevPageBtn");
		this.nextBtn = $("#nextPageBtn");
	},
	setCurPage : function(page){
		this.statics.currentPage = page;
	},
	getCurPage : function(){
		return this.statics.currentPage;
	},
	getData : function(){
		return this.statics.oData;
	},
	setData : function(id,data){
		if(id && data){
			this.statics.oData[id] = data;
		}else{
			this.statics.oData = {};
		}
	},
	onPrevNextClick : function(that,e){
		var tarBtn = $(e.currentTarget);
		var data_li = $(".pub_top_left").attr("data_li");
		if(tarBtn.hasClass("disable")) return false;
		if(data_li=="0"){
			var oData = that.statics.oData;
		}
		else{
			var dData = that.statics.dData;
		}
		var curPage = that.statics.currentPage;
		var toPage = tarBtn.hasClass("prevBtn") ? (curPage-1) : (curPage+1);
		
		if(data_li=="0"){
			if(tarBtn.hasClass("prevBtn")){
				that.nextBtn.removeClass("disable");
				that.statics.currentPage = toPage;
				//console.log(oData[toPage]);
				that.buildHtml(oData[toPage]);
				if(!that.statics.oData[toPage-1]) that.prevBtn.addClass("disable");
			}else{
				
				that.prevBtn.removeClass("disable");
				if(oData[toPage]){
					that.statics.currentPage = toPage;
					that.buildHtml(oData[toPage]);
					if(!that.statics.oData[toPage+1] && !that.statics.last){
						that.nextBtn.addClass("disable");
					}
				}else{
					that.fire("query_nexta",oData[curPage]["last"]);
				}
			}
		}
		else{
			if(tarBtn.hasClass("prevBtn")){
				that.nextBtn.removeClass("disable");
				that.statics.currentPage = toPage;
				that.buildHtml(dData[toPage]);
				if(!that.statics.dData[toPage-1]) that.prevBtn.addClass("disable");
			}else{
				
				that.prevBtn.removeClass("disable");
				if(dData[toPage]){
					that.statics.currentPage = toPage;
					that.buildHtml(dData[toPage]);
					if(!that.statics.dData[toPage+1] && !that.statics.last){
						that.nextBtn.addClass("disable");
					}
				}else{
					that.fire("query_nextb",dData[curPage]["last"]);
				}
			}
			
			
			
			
		}
	},
	query : function(opt){
		var that = this;
		var opt = opt || {};
		var pageSize = opt.pageSize || 15;
		var last = opt.last || "";
		var totalpage = opt.totalpage;
		var provice = opt.provice || "";
		//console.log(provice)
		var city = opt.city || "";
		var title = opt.title || "";
		var status = opt.status || "0";
		//console.log(title)
		var supplier = opt.supplier || "";
		var ttimeout = opt.ttimeout || 20 * 60 * 1000;
		var data_li = $(".pub_top_left").attr("data_li");
		var data = {
			pageSize : pageSize,
			last : last,
			title : supplier,
			action:"coupon_list",
			status:status
		};
		var listUl = $("#mlistUl");
		var pagenavW = $("#pagenavW");
		var status = this.statics.queryState;
		if(data_li=="0"){
			PFT.Ajax({
				url : "/call/jh_coupon.php",
				type : "GET",
				dataType : "json",
				ttimeout : ttimeout,
				data : data,
				loading : function(){
					listUl.html(status["loading"]);
					pagenavW.hide();
				},
				removeLoading : function(res){
					that.statics.last = "";
					that.statics.currentPage += 1;
					listUl.html("");
					pagenavW.show();
				},
				timeout : function(){ listUl.html(status["timeout"]);},
				serverError : function(){ listUl.html(status["serverError"]);} 
			},function(res){
				var code = res.status;
				var lists = res.list;
				var last = res.last;
				var totalPage = res.totalPage;
				if(code=="success"){
					if(!that.statics.isObjEmpty(lists)){
						that.statics.oData[that.statics.currentPage] = res;
						that.buildHtml(res);
					}else{
						listUl.html(status["empty"]);
						if(provice) that.fire("search_empty",res);
					}
					that.statics.last = last;
					if(!last){
						that.fire("no_next",res);
					}else{
						that.fire("has_next",res);
					}
				}else if(code==0){
					listUl.html(status["unlogin"]);
				}else{
					listUl.html(status["fail"]);
				}
			})
		}else{
			PFT.Ajax({
			url : "/call/jh_coupon.php",
			type : "GET",
			dataType : "json",
			ttimeout : ttimeout,
			data : data,
			loading : function(){
				listUl.html(status["loading"]);
				pagenavW.hide();
			},
			removeLoading : function(res){
				that.statics.last = "";
				that.statics.currentPage += 1;
				listUl.html("");
				pagenavW.show();
			},
			timeout : function(){ listUl.html(status["timeout"]);},
			serverError : function(){ listUl.html(status["serverError"]);} 
		},function(res){
			var code = res.status;
			var lists = res.list;
			var last = res.last;
			var totalPage = res.totalPage;
			if(code=="success"){
				if(!that.statics.isObjEmpty(lists)){
					that.statics.dData[that.statics.currentPage] = res;
					//console.log(that.statics.oData[that.statics.currentPage])
					//alert("200");
					that.abuildHtml(res);
				}else{
					listUl.html(status["empty"]);
					if(provice) that.fire("search_empty",res);
				}
				that.statics.last = last;
				if(!last){
					that.fire("no_next",res);
				}else{
					that.fire("has_next",res);
				}
			}else if(code==0){
				listUl.html(status["unlogin"]);
			}else{
				listUl.html(status["fail"]);
			}
		})
			
			
			
		}
	},
	abuildHtml : function(res){
		var that = this;
		var code = res.code;
		var lists = res.list;
		var str = "";
		var last = res.last;
		var data = lists;
		 str +=     '<thead >';
            str +=    ' <tr>';
            str +=        '<th class="setname">优惠券名称</th>';
            str +=        '<th class="setmoney">面额</th> ';
            str +=        '<th class="settype">发行方式</th>';
            str +=        '<th class="setAlign">使用渠道</th>';
            str +=       '<th class="settime">有限期限</th>';
            // str +=        '<th class="setdo">操作</th>';
            str +=      '</tr>';
            str +=    '</thead>';
		for(var i in data){
			is_data_empty = false;
			str += 		'<tbody class="tbody" id="tab">';
            str += 		'      <tr class="setbg_a">';
            str += 		'        <td class="setname" title="'+data[i]["coupon_name"]+'">'+data[i]["coupon_name"]+'</td>';
            str += 		'        <td class="setmoney">'+data[i]["coupon_value"]+'</td>';
            str += 		'        <td class="settype">领取</td>';
            str += 		'        <td class="setAlign set_type_l" at="'+data[i]["channel"]+'"><i class="i1"></i><i class="i2"></i><i class="i3"></i></td>';
            //str += 		'        <td class="settime">'+data[i]["use_startdate"]+'至</br>'+data[i]["use_enddate"]+'</td>';
            str += 		'        <td class="settime" data_time="'+data[i]["effective_type"]+'">';
            str += 		'       	<span class="bt_time">'+data[i]["use_startdate"]+'</span>';
			str += 		'			<span class="ed_time">'+data[i]["use_enddate"]+'</span>';
			str += 		'		</td>';
            // str += 		'       <td class="setdo">';
			// str += 		'			<span class="edit" ><a href="/publish_coupons.html?='+data[i]["id"]+'" target="_blank">编辑</a></span>';
			// str += 		'			<span class="user">适用产品</span>'; 
			// //str +=	'			<span class="console_l">领券情况</span>';
			// str += 		'			<span class="del" data_id="'+data[i]["id"]+'">下架</span>';
			// str += 		'		</td>';
            str += 		'      </tr>';
			str += 		'</tbody>';
			
			
		}
		$("#mlistUl").html(str);
		$(".set_type_l").each(function(){
			var at = $(this).attr("at");
			//console.log(at)
			var channel_len = at.split(",")
			var len = channel_len.length;
			for(var i=0;i<len;i++)
			 {
				var id = $(channel_len)[i];
				if(id=="1"){
					$(this).parents(".tbody").find(".i1").text(" 微信 ");
				}
				if(id=="2"){
					$(this).parents(".tbody").find(".i2").text(" 二级店铺 ");
				}
				if(id=="3"){
					$(this).parents(".tbody").find(".i3").text(" 自助机 ");
				}

			 }
		});
		
		
					
		
		
		
		$("#whichPageNum").text(that.statics.currentPage);

	},
	buildHtml : function(res){
		var that = this;
		var code = res.code;
		var lists = res.list;
		var str = "";
		var last = res.last;
		var data = lists;
		 str +=     '<thead >';
            str +=    ' <tr>';
            str +=        '<th class="setname">优惠券名称</th>';
            str +=        '<th class="setmoney">面额</th> ';
            str +=        '<th class="settype">发行方式</th>';
            str +=        '<th class="setAlign">使用渠道</th>';
            str +=       '<th class="settime">有限期限</th>';
            str +=        '<th class="setdo">操作</th>';
            str +=      '</tr>';
            str +=    '</thead>';
		for(var i in data){
			is_data_empty = false;
			str += 		'<tbody class="tbody" id="tab">';
            str += 		'      <tr class="setbg_a">';
            str += 		'        <td class="setname" title="'+data[i]["coupon_name"]+'">'+data[i]["coupon_name"]+'</td>';
            str += 		'        <td class="setmoney">'+data[i]["coupon_value"]+'</td>';
            str += 		'        <td class="settype">领取</td>';
            str += 		'        <td class="setAlign set_type_l" at="'+data[i]["channel"]+'"><i class="i1"></i><i class="i2"></i><i class="i3"></i></td>';
            //str += 		'        <td class="settime">'+data[i]["use_startdate"]+'至</br>'+data[i]["use_enddate"]+'</td>';
            str += 		'        <td class="settime"  data_time="'+data[i]["effective_type"]+'">';
            str += 		'       	<span class="bt_time">'+data[i]["use_startdate"]+'</span>';
			str += 		'			<span class="ed_time">'+data[i]["use_enddate"]+'</span>';
			str += 		'		</td>';
            str += 		'       <td class="setdo">';
			str += 		'			<span class="edit" ><a href="/publish_coupons.html?='+data[i]["id"]+'" target="_blank">编辑</a></span>';
			str += 		'			<span class="user" data_id="'+data[i]["pid"]+'">适用产品</span>'; 
			//str +=	'			<span class="console_l">领券情况</span>';
			str += 		'			<span class="del" data_id="'+data[i]["id"]+'">下架</span>';
			str += 		'		</td>';
            str += 		'      </tr>';
			str += 		'</tbody>'; 
			
			
		}
		$("#mlistUl").html(str);
		$(".set_type_l").each(function(){
			var at = $(this).attr("at");
			//console.log(at)
			var channel_len = at.split(",")
			var len = channel_len.length;
			for(var i=0;i<len;i++)
			 {
				var id = $(channel_len)[i];
				if(id=="1"){
					$(this).parents(".tbody").find(".i1").text(" 微信 ");
				}
				if(id=="2"){
					$(this).parents(".tbody").find(".i2").text(" 二级店铺 ");
				}
				if(id=="3"){
					$(this).parents(".tbody").find(".i3").text(" 自助机 ");
				}

			 }
		});
		
		
					
		
		
		
		$("#whichPageNum").text(that.statics.currentPage);

	}
	
	
	
	
	
	
});





